package main

import (
	"fmt"
	"sync"
)

var (
	s int
	m sync.RWMutex
)

func run() {
	var wg sync.WaitGroup
	//因为要监控110个协程，所以设置计数器为110
	wg.Add(110)
	for i := 0; i < 100; i++ {
		go func() {
			//计数器值减1
			defer wg.Done()
			Add(1)
		}()
	}

	for i := 0; i < 10; i++ {
		go func() {
			//计数器值减1
			defer wg.Done()
			fmt.Println("和为:", readS())
		}()
	}
	//一直等待，只要计数器值为0
	wg.Wait()
}

//func main() {
//	run()
//}

func Add(i int) int {
	m.Lock()
	defer m.Unlock()
	s += i
	return s
}

func readS() int {
	m.RLock()
	defer m.RUnlock()
	b := s
	return b
}
